package com.CmJava.utils;

import com.CmJava.entity.User;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

@Component
@PropertySource("classpath:application.yml")
public class RsaUtil {

    @Value("${Rsa.privateKey}")
    public String privateKey;

    public String publicKey="";

    public static KeyFactory keyFactory = null;

    static {
        try {
            keyFactory = KeyFactory.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public String getRsaToken(User user){
        RSAPublicKey rsaPublicKey = getRsaPublicKey();
        RSAPrivateKey rsaPrivateKey = getRsaPrivateKey();

        String token = JWT.create()
                .withClaim("userAccount", user.getUserAccount())    //载荷，随便写几个都可以
                .withClaim("userPassword", user.getUserPassword())    //载荷，随便写几个都可以
                .sign(Algorithm.RSA256(rsaPublicKey, rsaPrivateKey));
        return token;
    }

    //获取rsa公钥
    private RSAPublicKey getRsaPublicKey(){
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
        try {
            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
            return rsaPublicKey;
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取rsa私钥
    private RSAPrivateKey getRsaPrivateKey(){
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
        try {
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            return rsaPrivateKey;
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取HS256Token
    public static String getHS256Token(User user){
        String key="123456";

        String token = JWT.create()
                .withClaim("userAccount", user.getUserAccount())    //载荷，随便写几个都可以
                .withClaim("userPassword", user.getUserPassword())    //载荷，随便写几个都可以
                .sign(Algorithm.HMAC256(key));
        return token;
    }

    //解密
    public String decrypt(String encrypt){
        byte[] encryptByte = Base64.getDecoder().decode(encrypt);
        RSAPrivateKey rsaPrivateKey = getRsaPrivateKey();
        // RSA解密
        Cipher cipher = null;
        String password = null;
        try {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
            password = new String(cipher.doFinal(encryptByte));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return password;
    }
}
